package com.euq.dto;

import com.euq.entity.User;
import com.euq.enums.UserStatus;
import com.euq.utils.EuqException;
import com.euq.utils.MD5;
import org.apache.commons.lang3.StringUtils;

public class UserParam {
    public static void paramValidate(String account, String password){
        if (StringUtils.isBlank(account) || StringUtils.isBlank(password)){
            throw new EuqException("9999", "账号名密码不能为空");
        }
    }

    public static void businessValidate(User user, String password) throws Exception {
        if (user == null) {
            throw new EuqException("9999", "用户名或密码错误");
        }
        if(user.getStatus()==2){
            throw new EuqException("9999", "用户已冻结");
        }else if(user.getStatus()==3){
            throw new EuqException("9999", "用户已删除");
        }
        String passwordMD5= MD5.md5(password,user.getSalt());
        if(!user.getPassword().equals(passwordMD5)){
            throw new EuqException("9999", "用户名或密码错误");
        }
        if (StringUtils.isBlank(user.getRoleId())) {
            throw new EuqException("9999", "用户未配置权限");
        }

    }

    public static void idValidate(Long id){
        if(id==null){
            throw new EuqException("9999","用户ID不能为空");
        }
        if (id<=2){
            throw new EuqException("9999","不能删除初始用户");
        }
    }

    public static void roleIdValidate(Long id, String roleId){
        if (StringUtils.isBlank(roleId)) {
            throw new EuqException("9999","用户ID不能为空");
        }
        if (id==1){
            throw new EuqException("9999","不能修改超级管理员");
        }
    }

    public static void userStatusValidate(Long id,long status){
        if (id<=2){
            throw new EuqException("9999","不能冻结初始用户");
        }
        if(status!= UserStatus.ACTIVE.getValue()||status!=UserStatus.FROZEN.getValue()){
            throw new EuqException("9999","错误用户状态");
        }
    }

    public static void PwdValidate(String oldPwd, String newPwd, String rePwd){
        if(StringUtils.isEmpty(oldPwd)||StringUtils.isEmpty(newPwd)||StringUtils.isEmpty(rePwd)){
            throw new EuqException("9999","密码不能为空");
        } else if (!newPwd.equals(rePwd)) {
            throw new EuqException("9999","新密码前后不一致");
        }

    }
}
